﻿using System;
using System.Collections.Generic;
using ZyGames.DZPocker.Model;
using ZyGames.Framework.Cache.Generic;
using ZyGames.Framework.Game.Cache;
using ZyGames.Framework.Game.Context;
using ZyGames.Framework.Game.Contract;
using ZyGames.Framework.Game.Contract.Action;
using ZyGames.Framework.Game.Lang;
using ZyGames.Framework.Game.Service;
using System.Linq;
using ZyGames.DZPocker.Bll.Logic;
using ZyGames.DZPocker.Script;
using System.Text.RegularExpressions;
using ZyGames.Framework.Net;
using ZyGames.DZPocker.Tools;

namespace ZyGames.DZPocker.Script.CsScript.Action
{
	/// <summary>
	/// 移动端点击玩家头像并且下发弹出弹框，判断是否是好友，这个是根据客户端自己请求sdk来判断的，PC平台走自己的社交服务器，就发送30090协议
	/// </summary>
	public class Action30089 : BaseAction
	{
		private int tableId;
		private TableData _currentTable;
		/// <summary>
		/// 点击的人的userid
		/// </summary>
		private int clickUserId;
		/// <summary>
		/// 被点击的人的userid
		/// </summary>
		private int beClickedUserId;

		/// <summary>
		/// 是否是好友
		/// </summary>
		private bool isFriend;

		//是否是房主点击
		private bool isTableMasterClick = false;
		private bool isApplyChipWithoutCommitCanClick = true;
		private string beClickedUserNickName = string.Empty;
		//按钮类型   是添加还是删除
		private FriendShipButtonType FriendButtonType;

		public Action30089(HttpGet actionGetter)
			: base((short)ActionType.DZClickPlayerHeadIconOnMobile, actionGetter)
		{
		}
		public override bool GetUrlElement()
		{
			if (httpGet.GetInt("TableId", ref tableId) &&
				httpGet.GetInt("ClickedUserID", ref clickUserId) &&
				httpGet.GetInt("BeClickedUserID", ref beClickedUserId) &&
				httpGet.GetBool("IsFriend", ref isFriend))
			{
				return true;
			}
			return false;
		}

		public override bool TakeAction()
		{
			if (clickUserId == beClickedUserId)
			{
				Tool.WriteWarmingLine(string.Format("桌子:{0} 消息30090 两个人好友操作ID一样，不合法", tableId));
				return false;
			}
			_currentTable = GameRoom.Current.GetTableData(tableId);
			if (_currentTable == null)
			{
				Tool.WriteWarmingLine("消息30090获得房间不存在");
				return false;
			}
			var _beClickGameUser = GameTable.Current.GetUser(beClickedUserId);
			if(_beClickGameUser == null)
			{
				Tool.WriteErrorLine("消息30089获得的被点击的玩家不存在");
				return false;
			}
			//房主点击
			if (_currentTable.TableMaster.GetUserId() == this.clickUserId)
			{
				this.isApplyChipWithoutCommitCanClick = !_currentTable.ApplyChipWithoutConfirmUserIDList.Contains(beClickedUserId);
				this.isTableMasterClick = true;
			}
			else
				this.isTableMasterClick = false;
			if (isFriend)
			{
				this.FriendButtonType = FriendShipButtonType.Delete;
			}
			else
				this.FriendButtonType = FriendShipButtonType.Add;
			beClickedUserNickName = _beClickGameUser.NickName;
			return true;
		}

		public override void BuildPacket()
		{
			this.PushIntoStack(this.tableId);
			this.PushIntoStack((int)this.FriendButtonType);
			this.PushIntoStack(this.isTableMasterClick);
			this.PushIntoStack(this.isApplyChipWithoutCommitCanClick);
			this.PushIntoStack(this.beClickedUserId);
			this.PushIntoStack(this.beClickedUserNickName);
		}
	}
}
